<?php

if (! defined ( 'MEDIAWIKI' ))
    die ();

class Testwikirc extends SpecialPage {
       
     function __construct() {
                wfLoadExtensionMessages('Testwikirc');
                parent::__construct ( 'Testwikirc', 'testwikirc' );
    
    }

    public static function buildForm( $fields, $submitLabel = null ) {
        $form = '';
        $form .= "<table><tbody>";

        foreach( $fields as $labelmsg => $input ) {
            $id = "mw-$labelmsg";

            $form .= Xml::openElement( 'tr', array( 'id' => $id ) );
            if(strlen($input) > 0) {
                $form .= Xml::tags( 'td', array('class' => 'mw-label'), wfMsgExt( $labelmsg, array('parseinline') ) );
                $form .= Xml::openElement( 'td' ) . $input . Xml::closeElement( 'td' );
            } else {
                $form .= Xml::tags( 'td', array('class' => 'mw-label', 'colspan' => 2), wfMsgExt( $labelmsg, array('parseinline') ) );
            }
            $form .= Xml::closeElement( 'tr' );
        }

        $form .= "</tbody></table>";

        if ($submitLabel) {
            $form .= Xml::submitButton( wfMsg($submitLabel), array( 'id' => 'wpTestwikirc' ) );
        }

        return $form;
    }

    function showPage() {
        global $wgRequest, $wgOut, $wgUser, $wgTitle;
        $wgOut->addWikiText( wfMsg( 'testwikirc-header' ) );
        $wgOut->addWikiText( '[[Special:Testwikirc/on|' . wfMsg( 'testwikirc-onlink' ) . ']]' );
        $wgOut->addWikiText( '==' . wfMsg( 'testwikirc-sayheader' ) . '==' );

        $action = $wgTitle->escapeLocalUrl();
        $form = "<form name='request' action='$action' method='post'>";
        $formFields = array(
            'testwikirc-say' => wfInput('say'),
        );
        $form .= Testwikirc::BuildForm($formFields,'testwikirc-submit') . "</form>";
        $wgOut->addHTML( $form );
    }
    
    function doOn() {
      global $wgOut;
      
      $perl = popen( 'perl testwikirc.pl 2>&1', 'r' );
      pclose( $perl );
      $wgOut->addWikiText( wfMsg( 'testwikirc-on-success' ) );
      $this->showPage ();
    }

    function execute($subpage) {
        global $wgUser, $wgRequest, $wgOut, $wgUser, $wgTitle;
        
        $this->setHeaders ();
        
        if( !$wgUser->isAllowed( 'testwikirc' ) ) {

            $wgOut->permissionRequired( 'testwikirc' );
            return;
        }
        if ($subpage == 'on') {
            $this->doOn();
            return;
        }
        if ( $wgRequest->wasPosted() ) {
        $testwikircsaytext = $wgRequest->getText( "say" );
        $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
        socket_sendto( $conn, $testwikircsaytext, strlen($testwikircsaytext), 0, "localhost", 555111);
        socket_close($conn);
        
        $action = 'say';
        $message = $wgRequest->getText( "say" );
        $log = new LogPage( 'testwikirc' );
        $log->addEntry( $action, $wgUser->getUserPage(), $message, array( $wgUser->getId() ) );
        
        $wgOut->addWikiText( wfMsg( 'testwikirc-success' ) );
        $this->showPage ();

        } else {            
        $this->showPage ();
        }
        return;
    }

}